#include "qconnectionwidget.h"

QConnectionWidget::QConnectionWidget(QWidget *parent) :
  QWidget(parent)
{
  init();
}

void QConnectionWidget::incomingData(connectionInfo connInfo,QByteArray datain)
{
  emit dataIn(datain);
}

void QConnectionWidget::dataOut(QByteArray dataout)
{
  if(dataConn->getState()==ACTIVE)
  {
    dataConn->dataOut(dataout);
  }
}

void QConnectionWidget::toggleSource(int id)
{
  switch(id)
  {
  case 2:   // Serial
    addr_portLabel->setText("Serial Port");
    port_baudLabel->setText("Baud Rate");
    portSpin->setVisible(false);
    baudCombo->setVisible(true);
    break;
  default: // TCP / UDP
    addr_portLabel->setText("IP Address");
    port_baudLabel->setText("Port");
    baudCombo->setVisible(false);
    portSpin->setVisible(true);
    break;
  }
}

void QConnectionWidget::toggleConnection(bool on)
{
  if(on)
  {
    switch(sourceGroup->checkedId())
    {
    case 0: // TCP
      dataConn->setType(TCP);
      dataConn->setAddress_Port(addr_portEdit->text());
      dataConn->setPort_Baud(portSpin->value());
      dataConn->dataConnect();
      break;
    case 1: // UDP
      dataConn->setType(UDP);
      dataConn->setAddress_Port(addr_portEdit->text());
      dataConn->setPort_Baud(portSpin->value());
      dataConn->dataConnect();
      break;
      case 2: // SERIAL
      dataConn->setType(SERIAL);
      dataConn->setAddress_Port(addr_portEdit->text());
      dataConn->setPort_Baud(portSpin->value());
      dataConn->dataConnect();
    default:
      break;
    }
  }
  else
  {
    dataConn->dataDisconnect();
  }
}

void QConnectionWidget::serverDisconnect()
{
  connectButton->setChecked(false);
  connected=false;
  emit connectionChanged(false);
}

void QConnectionWidget::serverConnect()
{
  connected=true;
  emit connectionChanged(true);
}

void QConnectionWidget::init()
{
  // GUI Initialization
  // Source
  sourceBox = new QGroupBox("Source");
  sourceLayout = new QVBoxLayout;
  sourceGroup = new QButtonGroup;
  tcpButton = new QRadioButton("TCP");
  udpButton = new QRadioButton("UDP");
  serialButton = new QRadioButton("Serial");
  sourceGroup->addButton(tcpButton);
  sourceGroup->setId(tcpButton,0);
  sourceGroup->addButton(udpButton);
  sourceGroup->setId(udpButton,1);
  sourceGroup->addButton(serialButton);
  sourceGroup->setId(serialButton,2);
  tcpButton->setChecked(true);
  sourceLayout->addWidget(tcpButton);
  sourceLayout->addWidget(udpButton);
  sourceLayout->addWidget(serialButton);
  sourceBox->setLayout(sourceLayout);
  sourceBox->setFixedWidth(80);

  // Settings
  setbtnLayout = new QVBoxLayout;
  settingsLayout = new QGridLayout;
  port_baudLayout = new QHBoxLayout;
  addr_portLabel = new QLabel("IP Address");
  port_baudLabel = new QLabel("Port");
  addr_portEdit = new QLineEdit("192.168.0.193");
  addr_portEdit->setFixedSize(100,20);
  addr_portEdit->setAlignment(Qt::AlignCenter);
  portSpin = new QSpinBox;
  portSpin->setMinimum(1024);
  portSpin->setMaximum(65536);
  portSpin->setValue(50500);
  portSpin->setFixedSize(100,20);
  portSpin->setAlignment(Qt::AlignCenter);
  baudCombo = new QComboBox;
  baudCombo->addItem("4800");
  baudCombo->addItem("9600");
  baudCombo->addItem("19200");
  baudCombo->addItem("38400");
  baudCombo->addItem("57600");
  baudCombo->addItem("115200");
  baudCombo->setFixedSize(100,20);
  port_baudLayout->addWidget(portSpin);
  port_baudLayout->addWidget(baudCombo);
  baudCombo->setVisible(false);
  settingsLayout->addWidget(addr_portLabel,0,0,Qt::AlignLeft);
  settingsLayout->addWidget(addr_portEdit,0,1,Qt::AlignLeft);
  settingsLayout->addWidget(port_baudLabel,1,0,Qt::AlignLeft);
  settingsLayout->addLayout(port_baudLayout,1,1,Qt::AlignLeft);
  settingsLayout->setSpacing(8);

  // Button
  connectButton = new QPushButton("Connect");
  connectButton->setCheckable(true);

  setbtnLayout->addLayout(settingsLayout);
  setbtnLayout->addWidget(connectButton);

  // Main Layout
  mainLayout = new QHBoxLayout;
  mainLayout->addWidget(sourceBox);
  mainLayout->addLayout(setbtnLayout);
  this->setLayout(mainLayout);

  // Variable Initialization

  dataConn = new QDataConnection;

  // Connections
  connect(sourceGroup,SIGNAL(buttonClicked(int)),this,SLOT(toggleSource(int)));
  connect(connectButton,SIGNAL(toggled(bool)),this,SLOT(toggleConnection(bool)));
  connect(dataConn,SIGNAL(dataIn(connectionInfo,QByteArray)),this,SLOT(incomingData(connectionInfo,QByteArray)));
  connect(dataConn,SIGNAL(clientConnected()),this,SLOT(serverConnect()));
  connect(dataConn,SIGNAL(serverDisconnect()),this,SLOT(serverDisconnect()));

}
